package com.alibaba.dubbo.util;

import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

/**
 * Created by sunlei on 2015/7/17.
 * DubboConfig 工具类
 */
public class PropertiesHelper {

    /**
     * 从路径uri加载properties
     * @param uri
     * @return
     * @throws ClassNotFoundException
     * @throws IOException
     */
    public static Properties loadProperties(String uri) throws ClassNotFoundException, IOException {
        ClassLoader classLoader=Thread.currentThread().getContextClassLoader();
        if(classLoader==null) {
            throw new ClassNotFoundException("Could not find classloader");
        }

        InputStream in=classLoader.getResourceAsStream(uri);
        if(in==null){
            throw new IOException("Could not find class for resource:"+uri);
        }

        BufferedInputStream reader=null;
        try{
            reader=new BufferedInputStream(in);
            Properties properties=new Properties();
            properties.load(reader);
            return properties;
        }finally {
            try{
                reader.close();
            }catch(Exception e){

            }
        }
    }
}
